
package com.jinbo.footmessageworker.remark;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jinbo.footmessageworker.BaseActivity;
import com.jinbo.footmessageworker.MyApplication;
import com.jinbo.footmessageworker.R;
import com.jinbo.footmessageworker.turn.MoreTurnActivity;
import com.jinbo.footmessageworker.utils.DialogUtils;
import com.jinbo.footmessageworker.utils.KeyBoardUtils;
import com.jinbo.footmessageworker.utils.LocalPreferencesHelper;
import com.jinbo.footmessageworker.utils.TimeUtils;
import com.jinbo.footmessageworker.view.TitleBarView;

import java.util.Date;

public class SelectCustomerThingsActivity extends BaseActivity implements View.OnClickListener {

    CheckBox mCb1, mCb2,mCb3, mCb4,mCb5;
    TextView mTv1, mTv2, mTv3, mTv4, mTv5;
    TextView mNum1,mNum2,mNum3,mNum4,mNum5;
    View mV1,mV2,mV3,mV4,mV5;
    ImageView mAddIv1,mAddIv2,mAddIv3,mAddIv4,mAddIv5;
    ImageView mSubIv1, mSubIv2, mSubIv3, mSubIv4, mSubIv5;
    EditText mOtherEdt;

    private LocalPreferencesHelper mSharedPreference;
    private String mDataStr;

    public static void launch(Activity activity, int code) {

        Intent intent = new Intent(activity, SelectCustomerThingsActivity.class);

        activity.startActivityForResult(intent, code);

    }
    String user_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSharedPreference = MyApplication.local;//new LocalPreferencesHelper(this, LocalPreferencesHelper.DB_NAME);
        user_id= MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
        setContentView(R.layout.activity_select_customer_things);
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("顾客寄存备注");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        titleBarView.setRightTextIsVisible(true, "保存");
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_rightNextTextLayout, this);
        initView();
        setListener();

    }

    void initView() {
        //mRl1 = (RelativeLayout) findViewById(R.id.relative_layout_1);
        // mRl2 = (RelativeLayout) findViewById(R.id.relative_layout_2);
        mCb1 = (CheckBox) findViewById(R.id.check_box_1);
        mCb2 = (CheckBox) findViewById(R.id.check_box_2);
        mCb3 = (CheckBox) findViewById(R.id.check_box_3);
        mCb4 = (CheckBox) findViewById(R.id.check_box_4);
        mCb5 = (CheckBox) findViewById(R.id.check_box_5);

        mTv1 = (TextView) findViewById(R.id.text_view_1);
        mTv2 = (TextView) findViewById(R.id.text_view_2);
        mTv3 = (TextView) findViewById(R.id.text_view_3);
        mTv4 = (TextView) findViewById(R.id.text_view_4);
        mTv5 = (TextView) findViewById(R.id.text_view_5);

        mV1=findViewById(R.id.number_ll_1);
        mV2=findViewById(R.id.number_ll_2);
        mV3=findViewById(R.id.number_ll_3);
        mV4=findViewById(R.id.number_ll_4);
        mV5=findViewById(R.id.number_ll_5);


        mNum1=(TextView)findViewById(R.id.number_tv_1);
        mNum2=(TextView)findViewById(R.id.number_tv_2);
        mNum3=(TextView)findViewById(R.id.number_tv_3);
        mNum4=(TextView)findViewById(R.id.number_tv_4);
        mNum5=(TextView)findViewById(R.id.number_tv_5);

        mAddIv1=(ImageView)findViewById(R.id.number_add_1);
        mAddIv2=(ImageView)findViewById(R.id.number_add_2);
        mAddIv3=(ImageView)findViewById(R.id.number_add_3);
        mAddIv4=(ImageView)findViewById(R.id.number_add_4);
        mAddIv5=(ImageView)findViewById(R.id.number_add_5);

        mSubIv1=(ImageView)findViewById(R.id.number_sub_1);
        mSubIv2=(ImageView)findViewById(R.id.number_sub_2);
        mSubIv3=(ImageView)findViewById(R.id.number_sub_3);
        mSubIv4=(ImageView)findViewById(R.id.number_sub_4);
        mSubIv5=(ImageView)findViewById(R.id.number_sub_5);


        mOtherEdt = (EditText) findViewById(R.id.edt_other);
        mDataStr = mSharedPreference.getString(user_id+LocalPreferencesHelper.REMARK_CUSTOMER);
        if (!TextUtils.isEmpty(mDataStr)) {
            if(!checkedNumber(mTv1.getText().toString()).equals("-1")) {
                mCb1.setChecked(true);
                mV1.setVisibility(View.VISIBLE);
                mNum1.setText(checkedNumber(mTv1.getText().toString()));
            }
            if(!checkedNumber(mTv2.getText().toString()).equals("-1")) {
                mCb2.setChecked(true);
                mV2.setVisibility(View.VISIBLE);
                mNum2.setText(checkedNumber(mTv2.getText().toString()));
            }
            if(!checkedNumber(mTv3.getText().toString()).equals("-1")) {
                mCb3.setChecked(true);
                mV3.setVisibility(View.VISIBLE);
                mNum3.setText(checkedNumber(mTv3.getText().toString()));
            }
            if(!checkedNumber(mTv4.getText().toString()).equals("-1")) {
                mCb4.setChecked(true);
                mV4.setVisibility(View.VISIBLE);
                mNum4.setText(checkedNumber(mTv4.getText().toString()));
            }
            if(!checkedNumber(mTv5.getText().toString()).equals("-1")) {
                mCb5.setChecked(true);
                mV5.setVisibility(View.VISIBLE);
                mNum5.setText(checkedNumber(mTv5.getText().toString()));
            }
            mOtherEdt.setText(getOtherStr());

        }
    }

    void setListener() {
        mCb1.setOnClickListener(this);
        mCb2.setOnClickListener(this);
        mCb3.setOnClickListener(this);
        mCb4.setOnClickListener(this);
        mCb5.setOnClickListener(this);

        mAddIv1.setOnClickListener(this);
        mAddIv2.setOnClickListener(this);
        mAddIv3.setOnClickListener(this);
        mAddIv4.setOnClickListener(this);
        mAddIv5.setOnClickListener(this);



        mSubIv1.setOnClickListener(this);
        mSubIv2.setOnClickListener(this);
        mSubIv3.setOnClickListener(this);
        mSubIv4.setOnClickListener(this);
        mSubIv5.setOnClickListener(this);


        //mRl1.setOnClickListener(this);
        // mRl2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int num;
        switch (view.getId()) {
            case R.id.left_back_layout:
               /* new DialogUtils(this, "center", true, 0)
                        .setTitle("提示")
                        .setMessage("是否确定放弃本次备注操作")
                        .setButtons("确定", "取消",
                                new DialogUtils.OnDialogButtonClickListener() {
                                    @Override
                                    public void onDialogButtonClick(Context context, DialogUtils builder, Dialog dialog, int dialogId, int which) {
                                        if (which == DialogUtils.OnDialogButtonClickListener.BUTTON_LEFT) {
                                            finish();
                                        }
                                    }
                                }).create().show();*/
                finish();
                break;
            case R.id.check_box_1:
                changeState(mCb1, mV1);

                // mV1.setVisibility(View.VISIBLE);
                break;
            case R.id.check_box_2:
                changeState(mCb2, mV2);
                //  changeState(mCb2);
                break;
            case R.id.check_box_3:
                changeState(mCb3, mV3);
                // changeState(mCb3);
                break;
            case R.id.check_box_4:
                changeState(mCb4, mV4);
                // changeState(mCb4);
                break;
            case R.id.check_box_5:
                changeState(mCb5, mV5);
                //changeState(mCb5);
                break;
            case R.id.number_add_1:
                num=Integer.valueOf(mNum1.getText().toString())+1;
                mNum1.setText(num + "");
                break;
            case R.id.number_sub_1:
                num=Integer.valueOf(mNum1.getText().toString())-1;
                mNum1.setText(num + "");
                break;

            case R.id.number_add_2:
                num=Integer.valueOf(mNum2.getText().toString())+1;
                mNum2.setText(num + "");
                break;
            case R.id.number_sub_2:
                num=Integer.valueOf(mNum2.getText().toString())-1;
                mNum2.setText(num+"");
                break;

            case R.id.number_add_3:
                num=Integer.valueOf(mNum3.getText().toString())+1;
                mNum3.setText(num + "");
                break;
            case R.id.number_sub_3:
                num=Integer.valueOf(mNum3.getText().toString())-1;
                mNum3.setText(num+"");
                break;

            case R.id.number_add_4:
                num=Integer.valueOf(mNum4.getText().toString())+1;
                mNum4.setText(num + "");
                break;
            case R.id.number_sub_4:
                num=Integer.valueOf(mNum4.getText().toString())-1;
                mNum4.setText(num+"");
                break;

            case R.id.number_add_5:
                num=Integer.valueOf(mNum5.getText().toString())+1;
                mNum5.setText(num + "");
                break;
            case R.id.number_sub_5:
                num=Integer.valueOf(mNum5.getText().toString())-1;
                mNum5.setText(num+"");
                break;

            case R.id.right_next_text_layout:
                String remarkData = getRemarkData();
                if (TextUtils.isEmpty(remarkData)) {//seleteType.equals("")||
                    new DialogUtils(this, "center", true, 0).setTitle("提示")
                            .setMessage("你还未备注，请备注")
                            .setButtons(null, "确定", null).create().show();
                } else {

                    String remarkDate = TimeUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss");

                    mSharedPreference.saveOrUpdate(user_id+LocalPreferencesHelper.REMARK_TIME_CUSTOMER, remarkDate);
                    mSharedPreference.saveOrUpdate(user_id+LocalPreferencesHelper.REMARK_CUSTOMER, remarkData);
                    Intent data = new Intent();
                    setResult(RESULT_OK, data);
                    finish();
                }
                break;

        }
    }


    // 改变选择按钮状态
    public void changeState(CheckBox ck,View v) {
        if (mOtherEdt.isFocused()) {
            mOtherEdt.clearFocus();
            KeyBoardUtils.closeKeybord(this, mOtherEdt);
        }
        // isChange=true;
        if (ck.isChecked()) {
            // ck.setChecked(false);
            v.setVisibility(View.VISIBLE);
        } else {
            v.setVisibility(View.INVISIBLE);
            //ck.setChecked(true);
        }
    }

    // 选择备注的数据，以“/”隔开,（第一个一定是其他输入框，有则是字符串，没有就是“-1”）
    public String getRemarkData() {
        StringBuffer mRemark = new StringBuffer();
        String s = mOtherEdt.getText().toString();
        //String num="";
        if (!TextUtils.isEmpty(s)) {
            mRemark.append(s.replace('/',' ') + "/");
        } else {
            mRemark.append("-1/");
        }

        if (mCb1.isChecked()) {
            mRemark.append(mTv1.getText() +"×" +mNum1.getText()+"/");
        }
        if (mCb2.isChecked()) {
            mRemark.append(mTv2.getText() +"×" +mNum2.getText()+"/");
        }
        if (mCb3.isChecked()) {
            mRemark.append(mTv3.getText() +"×" +mNum3.getText()+"/");
        }
        if (mCb4.isChecked()) {
            mRemark.append(mTv4.getText() +"×" +mNum4.getText()+"/");
        }
        if (mCb5.isChecked()) {
            mRemark.append(mTv5.getText() +"×" +mNum5.getText()+"/");
        }
        if (TextUtils.isEmpty(mRemark))
            return null;
        else {
            if (mRemark.toString().equals("-1/")) {
                return null;
            } else
                return mRemark.toString();
        }
    }


    public String checkedNumber(String type) {
        if (TextUtils.isEmpty(mDataStr)) {
            return "-1";
        } else {
            String[] typeStr = mDataStr.split("/");
            for (int i = 1; i < typeStr.length; i++) {
                String[] name=typeStr[i].split("×");
                if (name[0].equals(type)) {
                    return name[1];
                }
            }
        }
        return "-1";
    }

    public String getOtherStr() {
        if (TextUtils.isEmpty(mDataStr)) {
            return null;
        } else {
            String[] typeStr = mDataStr.split("/");
            if (!typeStr[0].equals("-1"))
                return typeStr[0];
            else
                return null;
        }
    }
}
